package com.crane.oss.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.enums.DelFlagEnum;
import com.crane.oss.school.bean.request.QueryTeacherClassReq;
import com.crane.oss.school.bean.request.teacherclass.AddTeacherClassReq;
import com.crane.oss.school.bean.request.teacherclass.UpdateTeacherClassReq;
import com.crane.oss.school.bean.resp.TeacherClassResp;
import com.crane.oss.school.dao.TeacherClassDao;
import com.crane.oss.school.model.TeacherClass;
import com.crane.oss.school.service.TeacherClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import javax.annotation.Resource;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */
@Service
@Slf4j
public class TeacherClassServiceImpl implements TeacherClassService {

    @Resource
    protected TeacherClassDao teacherClassDao;

    @Override
    public TeacherClassResp queryTeacherClass(Long id) {
        log.info("TeacherClassBOImpl queryTeacherClass id={}", id);
        TeacherClass teacherClass = teacherClassDao.selectById(id);
        if (null == teacherClass) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "teacherClass");
        }
        TeacherClassResp teacherClassResp = new TeacherClassResp();
        BeanUtil.copyProperties(teacherClass, teacherClassResp);
        return teacherClassResp;
    }

    @Override
    public IPage<TeacherClassResp> queryList(QueryTeacherClassReq queryRequest) {
        log.info("TeacherClassBOImpl queryList queryRequest={}", queryRequest);
        IPage<TeacherClassResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<TeacherClass> queryTeacherClassWrapper = new LambdaQueryWrapper<>();
        IPage<TeacherClass> page = teacherClassDao.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryTeacherClassWrapper);
        return page.convert(u -> {
            TeacherClassResp v = new TeacherClassResp();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddTeacherClassReq addRequest) {
        log.info("TeacherClassBOImpl save request={}", addRequest);
        TeacherClass teacherClass = new TeacherClass();
        BeanUtil.copyProperties(addRequest, teacherClass);
        int i = 0;
        try {
            i = teacherClassDao.insert(teacherClass);
        } catch (Exception e) {
            log.info("TeacherClassBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateTeacherClassReq updateRequest) {
        log.info("TeacherClassServiceImpl update request={}", updateRequest);
        TeacherClass teacherClass = new TeacherClass();
        BeanUtil.copyProperties(updateRequest, teacherClass);
        int i = 0;
        try {
            i = teacherClassDao.updateById(teacherClass);
        } catch (Exception e) {
            log.info("TeacherClassServiceImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("TeacherClassServiceImpl delete id={}", id);
        return teacherClassDao.deleteById(id);
    }

	@Override
	public List<TeacherClassResp> queryListByClassId(Long classId) {
		 log.info("TeacherClassServiceImpl queryListByClassId classId={}", classId);
     	List<TeacherClass> queryTeacherClassList = teacherClassDao.selectList(Wrappers.lambdaQuery(TeacherClass.class)
    			.eq(TeacherClass::getClassId, classId)
    			.eq(TeacherClass::getDelFlag, DelFlagEnum.NO.getType()));
     	List<TeacherClassResp> returnList = BeanUtil.copyList(queryTeacherClassList, TeacherClassResp.class);
		return returnList;
	}

}
